﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;

namespace MSDIS.Configuration
{
    internal class Utils
    {
        private static MemoryStream memoryStream = new MemoryStream();
        private static BinaryFormatter binaryFormatter = new BinaryFormatter();

        internal static byte[] GetValueAsBinary(object variable)
        {
            byte[] result = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                binaryFormatter.Serialize(memoryStream, variable);
                result = memoryStream.ToArray();
            }
            return result;
        }

        internal static Object GetValue(byte[] receivedBytes)
        {
            object result = null;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                memoryStream.Write(receivedBytes, 0, receivedBytes.Length);
                memoryStream.Seek(0, SeekOrigin.Begin);
                result = (Object)binaryFormatter.Deserialize(memoryStream);

            }
            return result;
        }

        internal static string CreateConnectionString(string dbConnectionString)
        {
            string connectionString =
                "metadata="
                + "res://*/Models.Model.csdl|res://*/Models.Model.ssdl|res://*/Models.Model.msl;"
                + "provider=System.Data.SqlClient;"
                + "provider connection string='"
                + dbConnectionString
                + ";MultipleActiveResultSets=True;App=EntityFramework'";

            return connectionString;
        }
    }
}
